﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Puzzler.Plugin.Interface;
using Puzzler.XML;

namespace Puzzler.Plugin
{
    public sealed class PluginObject
    {
        public PuzzleInfo PuzzleInfo { get; private set; }
        public Type PuzzleClass { get; private set; }
        public XWizard WizardXml { get; private set; }
        public XRibbonExtension RibbonExtension { get; private set; }

        internal PluginObject(PuzzleInfo puzzleInfo, Type puzzleClass, XWizard wizardXml, XRibbonExtension ribbonExtension)
        {
            PuzzleInfo = puzzleInfo;
            PuzzleClass = puzzleClass;
            WizardXml = wizardXml;
            RibbonExtension = ribbonExtension;
        }

        public IPuzzlePlugin CreatePluginInstance()
        {
            return (IPuzzlePlugin) Activator.CreateInstance(PuzzleClass);
        }

        private bool Equals(PluginObject other)
        {
            return PuzzleInfo.Equals(other.PuzzleInfo) && PuzzleClass == other.PuzzleClass;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj is PluginObject && Equals((PluginObject) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (PuzzleInfo.GetHashCode()*397) ^ (PuzzleClass != null ? PuzzleClass.GetHashCode() : 0);
            }
        }

        public override string ToString()
        {
            return string.Format("PuzzleInfo: {0}, PuzzleClass: {1}", PuzzleInfo, PuzzleClass);
        }
    }
}
